home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Info-Mac 4
/
Info_Mac IV CD-ROM (Pacific HiTech Inc.)(August 1994).iso
/
Development
/
Source
/
MacStarter (THINK C 5.0⁄6.0)
/
other applicationProcs files
/
scrollDemo.c
< prev
next >
Wrap
Text File
|
1994-01-25
|
9KB
|
254 lines
/* This file demonstrates simple use of scroll bars in MacStarter.
Just for fun, it also illustrates how to change the cursor style.
To use this file, you should add it to the MacStarter.π project
and delete the current applicationProcs file.
All places where this file has been modified (from the
original applicationProcs.c) are commented with comments
that begin with //
*/
#include "globals-MacStarter.h"
long gEventWaitTime = 3; // a value of 3 indicates that this program
// wants function ApplicationIdle to be called
// every 3/60-th seconds (if possible). This
// seems to be a reasonable interval when
// ApplicationIdle is setting the cursor style.
CursHandle fingerCursor; // global variable for a cursor that looks
// like a pointing hand. This cursor is loaded
// in InitApplication
MenuHandle editMenu, fileMenu;
void InitApplication(void);
void UpdateMenus(void);
void DoEditMenu(int itemNum);
void DoFileMenu(int itemNum, int* done);
void DoOtherMenu(int menuID, int itemNum);
void ApplicationIdle(void);
void CleanUpApplication(void);
void AboutBox(void);
void DoNewCommand(void);
class myWindow : public xWindow {
public:
virtual void OpenInRect(Str255 title, int left, int top, int right, int bottom);
virtual short Close(void);
protected:
virtual void SetDefaults(void);
virtual void doKey(char ch);
virtual void doContentClick(Point localPt);
virtual void adjustToNewSize(void);
virtual void doRedraw(Rect* badRect);
virtual void doHScroll(int dh);
virtual void doVScroll(int dv);
virtual void doActivate(int active);
};
void myWindow::SetDefaults(void) {
inherited::SetDefaults();
hLinesPerPage = 15; // A click in the gray area of the horizontal scroll
// bar will have an effect 15 times as big as a
// click on on arrow.
vLinesPerPage = 15; // Similarly for the vertical scroll bar.
}
void myWindow::OpenInRect(Str255 title, int left, int top, int right, int bottom) {
inherited::OpenInRect(title,left,top,right,bottom);
}
short myWindow::Close(void) {
inherited::Close();
}
void myWindow::doKey(char ch) {
}
void myWindow::doContentClick(Point localPt) {
}
// Function doRedraw draws a rectangle whose height and width depend
// on the current scroll bar settings. Since this procedure is
// called by doHScroll or doVScroll when the user clicks on the scroll bar,
// the size of the rectangle changes while the user continues to scroll.
// Note the flickering effect caused by the use of the default doHScroll
// and doVScroll, which erase the screen before calling doRedraw.
void myWindow::doRedraw(Rect* badRect){
Rect R;
short hVal,vVal;
R = theWindow->portRect; // this is a rectangle that covers the whole window
R.bottom = R.bottom - 15; // don't include space occupied by horizontal scroll
R.right = R.right - 15; // or vertical scroll
hVal = GetHVal(); // get current setting of horizontal scroll bar
vVal = GetVVal(); // get current setting of vertical scroll bar
InsetRect(&R,hVal,vVal); // remove a border from around the rectangle
// with size depending on hVal and vVal
PaintRect(&R); // Fill in the remaining rectangle
}
// Sets the maximum values on the horizontal and vertical scrolls to
// appropriate values for the window.
void myWindow::adjustToNewSize(void) {
Rect R;
short height,width;
inherited::adjustToNewSize();
R = theWindow->portRect; // this is a rectangle that covers the whole window
R.bottom = R.bottom - 15; // don't include space occupied by horizontal scroll
R.right = R.right - 15; // or vertical scroll
height = R.bottom - R.top; // height of window
width = R.right - R.left; // width of window
SetHMax((width-1) / 2); // set maximum possible value of scroll bars
SetVMax((height-1) / 2);
// NOTE: The values used here are simply the largest values by which
// the rectangel can be "inset" in function doRedraw without
// actually making the rectangle disappear.
}
void myWindow::doHScroll(int dh) {
inherited::doHScroll(dh);
}
void myWindow::doVScroll(int dv) {
inherited::doVScroll(dv);
}
void myWindow::doActivate(int active) {
inherited::doActivate(active);
}
void InitApplication(void) {
MenuHandle appleMenu;
fingerCursor = GetCursor(129); // load the "pointing finger cursor"
// from the resource file.
fileMenu = GetMHandle(2);
editMenu = GetMHandle(3);
appleMenu = GetMHandle(1);
SetItem(appleMenu,1,"\pAbout StupidScroll..."); // Sets the name of the
DoNewCommand(); // program in first line of
} // the Apple menu
void UpdateMenus(void) {
short i;
WindowPtr win;
xWindow *xwin;
win = FrontWindow();
if ( win && ((WindowPeek)win)->windowKind < 0 ) {
EnableItem(editMenu,1);
for (i=3; i<7; i++)
EnableItem(editMenu,i);
}
else {
DisableItem(editMenu,1);
for (i=3; i<7; i++)
DisableItem(editMenu,i);
}
if (win && xWindow::Window2XWindow(win,&xwin)) {
EnableItem(fileMenu,2);
}
else {
DisableItem(fileMenu,2);
}
}
void DoEditMenu(int itemNum) {
}
void DoFileMenu(int itemNum, int* done) {
xWindow *win;
if (itemNum == 4)
*done = 1;
else if (itemNum == 1)
DoNewCommand();
else if (itemNum == 2 && xWindow::Window2XWindow(FrontWindow(),&win))
win->Close();
}
void DoOtherMenu(int menuID, int itemNum) {
}
// ApplicationIdle checks the current mouse position and uses it to determine
// which style of cursor should be used. In this program, the arrow
// cursor is used except when the mouse is over a scroll bar; in that
// case, a pointing finger cursor is used.
// Hint: If you get serious about cursor-setting, you will probably
// want to put a SetCursor(&arrow) at the beginning of each menu-handling
// procedure. Otherwise, you can get stuck with a funny cursor while
// a menu command is being carried out. (This is annoying if the
// command brings up a dialog box.) Note that "arrow" is a predefined
// variable of type Cursor.
void ApplicationIdle(void) {
Point pt; // mouse location
WindowPtr win; // window contining mouse (if any)
xWindow *xwin; // the xWindow corresponding to win
short partNum; // Returned by FindWindow to tell what the
// mouse is currently over.
GrafPtr savePort; // I need this because I have to set the drawing
// port in order to use the function GlobalToLocal
ControlHandle theControl; // If the mouse is over a window, I have to
// check whether it is over a "control"
// which in this program can only be a
// scroll bar in the frontmost window.
pt = gEvent.where; // mouse loc is taken from global event record gEvent
// (This might be slightly out-of-date; you could
// get a new event record using EventAvail() if you want.)
partNum = FindWindow(pt, &win); // Sets win=0 if the mouse is not
// over a window; if it is, partNum will have the
// value inContent when the mouse is over the inside
// of the window.
if ( win && partNum == inContent && xWindow::Window2XWindow(win,&xwin) ) {
// The last check is here because I want to ignore any
// window that wasn't created by this program.
GetPort(&savePort);
SetPort(win);
GlobalToLocal(&pt); // FindControl requires a point in window coords
SetPort(savePort);
FindControl(pt,win,&theControl);
// if the mouse is over a control, FindControl sets
// theControl to be a handle to that control, hence
// non-zero. I also check that fingerCursor is non-zero;
// It would be zero if for some reason the cursor couldn't
// loaded from the resource file.
SetCursor( (theControl && fingerCursor) ? *fingerCursor : &arrow );
}
else
SetCursor(&arrow); // Make sure to set the cursor to something or
// other in this function. (It is easier to
// think in terms of continually "setting"
// the cursor, rather than trying to "change"
// it only when necessary.)
}
void CleanUpApplication(void) {
}
// Set up the About Box to describe this program
void AboutBox(void) {
ParamText( "\pScroll Demo",
"\pDavid Eck",
"\pHobart and William Smith College\rGeneva, NY 14456\rE-mail: eck@hws.bitnet",
"\pDemonstrates the use of scroll bars in an application written using the Macintosh application shell MacStarter.");
Alert(128,0L);
}
void DoNewCommand(void) {
myWindow *win;
win = new myWindow;
win->Open("\pSample Window");
}